using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_EquipInfoReborn : UIBaseNode
    {

        #region Template Generate,don't modify
        protected class UIB_UIN_EquipInfoReborn
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExButton buttonRight { protected set; get; }
            public UnityEngine.GameObject commonEquip { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonLeft { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textNameNum { protected set; get; }
            public UnityEngine.GameObject commonEquipReborn { protected set; get; }
            public UnityEngine.GameObject commonItem { protected set; get; }
            public UnityEngine.GameObject costMoney { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnRecast { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonRight", out var __tbv0);
                this.buttonRight = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("commonEquip", out var __tbv1);
                this.commonEquip = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonLeft", out var __tbv2);
                this.buttonLeft = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textNameNum", out var __tbv3);
                this.textNameNum = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("commonEquipReborn", out var __tbv4);
                this.commonEquipReborn = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("commonItem", out var __tbv5);
                this.commonItem = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("costMoney", out var __tbv6);
                this.costMoney = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnRecast", out var __tbv7);
                this.btnRecast = __tbv7;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        UIN_CommonEquip equip = null;
        UIN_CommonEquip equipReborn = null;
        UIN_CostMoney costMoney = null;
        ListScrollAdapter<UIN_OneItem> items = null;

        UD_EquipInfo udEquip = null;
        int equipCount = 0;
        int equipIndex = 0;
        Dictionary<int, int> dicReCost = null;
        List<int> recosts = ListPool<int>.Get();
        int cntEquip = 0;
        int cntItems = 0;

        protected UIB_UIN_EquipInfoReborn ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_EquipInfoReborn();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.ui.buttonRight.onClick.AddListener(this.OnClickRight);
            this.ui.buttonLeft.onClick.AddListener(this.OnClickLeft);
            this.ui.btnRecast.onClick.AddListener(this.OnClickReborn);

            this.equip = this.InitChildNode<UIN_CommonEquip>(this.ui.commonEquip);
            this.equip.SetViewActive(false);
            this.equipReborn = this.InitChildNode<UIN_CommonEquip>(this.ui.commonEquipReborn);
            this.equipReborn.SetViewActive(false);


            this.costMoney = this.InitChildNode<UIN_CostMoney>(this.ui.costMoney);
            this.items = this.InitListScroll<UIN_OneItem>(this.ui.commonItem, this.OnItemChanged);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        #region 单击
        private void OnClickRight()
        {
            var ui = this.GetBelongUI();
            ui?.ChangeEquip(true);
        }

        private UIEquipInfo GetBelongUI()
        {
            return this.belongWindow as UIEquipInfo;
        }

        private void OnClickLeft()
        {
            var ui = this.GetBelongUI();
            ui?.ChangeEquip(false);
        }

        void OnClickReborn()
        {
            this.SendReborn().Forget();
        }

        async UniTaskVoid SendReborn()
        {
            var result = await EquipmentModule.NetCore.CS_reborn(this.udEquip.equipItem.id);
            if (result.IsSuccess)
            {
                NoticeModule.ShowUIItemGet(result.awardPods);
                this.Close();
            }
        }
        #endregion

        protected override void OnDispose()
        {
            this.equip = null;
            this.equipReborn = null;
            this.costMoney = null;
            this.items = null;

            this.udEquip = null;
            this.equipIndex = 0;
            this.equipCount = 0;

            if (this.recosts != null)
                ListPool<int>.Put(this.recosts);
            this.recosts = null;
        }

        public void Show(UD_EquipInfo udEquip)
        {
            this.Show();
            this.udEquip = udEquip;
            this.equipCount = udEquip.equipCount;
            this.equipIndex = udEquip.equipIndex;

            this.Refresh();
        }

        void Refresh(ItemEquipmentData equipItem = null)
        {
            equipItem = equipItem == null ? this.udEquip.equipItem : equipItem;
            this.udEquip.ChangeEquip(equipItem);
            this.equip.Show(equipItem);
            this.equipReborn.ShowOneLvOneStar(equipItem);
            this.CalcRebornReCosts();

            this.ViewBtn();
            this.ViewReEquips();
            this.ViewReItems();
            this.ViewCostMoney();
        }

        void ViewBtn()
        {
            this.ui.buttonRight.SetActive(this.equipIndex < this.equipCount - 1);
            this.ui.buttonLeft.SetActive(this.equipIndex > 0);
        }

        void CalcRebornReCosts()
        {
            this.recosts.Clear();
            this.cntEquip = 0;
            this.cntItems = 0;

            var equipItem = this.udEquip.equipItem;
            this.dicReCost = EquipmentModule.Instance.CalcRebornCosts(equipItem);
            if (this.dicReCost != null)
            {
                if (this.dicReCost.TryGetValue(equipItem.cid, out this.cntEquip))
                    this.dicReCost.Remove(equipItem.cid);

                this.recosts.AddRange(this.dicReCost.Keys);
                this.cntItems = this.recosts.Count;
            }
        }

        void ViewReEquips()
        {
            var cfgItem = this.udEquip.equipItem.cfgData;
            this.ui.textNameNum.text = string.Format(APIUIEquip.FmtRebornEquipNameNum, cfgItem.Name, this.cntEquip);
        }

        void ViewReItems()
        {
            this.items.RefillCells(this.cntItems);
        }

        void OnItemChanged(UIN_OneItem item, int index)
        {
            int itemCid = this.recosts[index];
            int itemNum = this.dicReCost[itemCid];
            var cfg = TableCenter.item.Get(itemCid);
            item.Show(cfg);
            item.SetCount(itemNum, true);
        }

        void ViewCostMoney()
        {
            var equipData = this.udEquip.equipItem;
            var cfgStar = equipData.cfgStar;
            if (cfgStar.RebornCostItems.Count == 2)
                this.costMoney.Show(cfgStar.RebornCostItems[0], cfgStar.RebornCostItems[1]);
            else
                this.costMoney.Hide();
        }
    }
}
